#include"JsObject.h"
#include"JsContext.h"
#include"JsEngine.h"
#include"JsVm.h"
#include"JsValue.h"
#include"JsList.h"
#include"JsSys.h"
#include"JsInit.h"
#include"JsFunction.h"
#include"JsDebug.h"
#include"JsAst.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>


struct JsObject* JsCreateStandardFunction(enum JsFunctionType type,int argc, 
		char** argv,void* data,JsFunctionFn fn,char* name,int sync,JsList scope){

	struct JsObject* object ;
	struct JsFunction* function = (struct JsFunction* )JsMalloc(sizeof(struct JsFunction));
	//先配置JsObject信息	
	object = JsCreateStandardFunctionObject(scope);
	
	object->Value = function;
	//配置Function 内置属性
	function->type = type;
	function->argc = argc; 
	function->argv = argv;
	function->data = data;
	function->fn = fn;
	function->name = name;
	function->sync = sync;
	if(sync){
		JsCreateLock(function->syncLock);
	}
	
	//配置Function Object显示属性
	struct JsValue* length = (struct JsValue*)JsMalloc(sizeof(struct JsValue));
	length->type = JS_NUMBER;
	length->u.number = argc;
	(*object->Put)(object,"length",length,JS_OBJECT_ATTR_STRICT);
			
			
	struct JsObject* prototypeObj = JsCreateStandardObject();
	struct JsValue* constructor = (struct JsValue*)JsMalloc(sizeof(struct JsValue));
	constructor->type = JS_OBJECT;
	constructor->u.object = object;
	(*prototypeObj->Put)(prototypeObj,"constructor",constructor,JS_OBJECT_ATTR_DONTENUM);
	
	struct JsValue* prototype = (struct JsValue*)JsMalloc(sizeof(struct JsValue));
	prototype->type = JS_OBJECT;
	prototype->u.object = prototypeObj;
	(*object->Put)(object,"prototype",prototype,JS_OBJECT_ATTR_DONTDELETE);

	return object;
}
